<?php

class McCube_Html_Form_Element_MultiSelect extends McCube_Html_Form_Element{

    protected $_valueKey   = '';
    protected $_contentKey = '';
    protected $_options    = array();
    protected $_fields     = array();

    public function __construct($name, $config=array()){
        parent::__construct($name, $config);

        $this->_fields['select'][] = new McCube_Html_Form_Element_Select($this->name.'[select][1]');
        $this->_fields['select'][] = new McCube_Html_Form_Element_Select($this->name.'[select][2]');
        $this->_fields['button'] = new McCube_Html_Form_Element_Input('button', '');
        $this->_fields['button']->setValue('Ajouter');
        $this->_fields['button']->setAttr('onclick'
            , '$.mcCube.form.element.multiText.addItem(\''.$this->id.'\',\''.$this->name.'\');'
        );
    }

    public function setOptions($options=array()){
        foreach($this->_fields['select'] as $key=>$field){
            if(!$options[$key]){
                if(isset($this->_config['datas']['db'][$key])){
                    $options[$key] = $this->getDatasOnDb($this->_config['datas']['db'][$key]);
                }
            }
            $field->setOptions($options[$key]);
        }
    }

    /** */
    protected function _build(){
        if(!$this->_options)
            $this->setOptions();

        return '<div id="'.$this->id.'_div">'
            .$this->_fields['select'][0]->render()
            .$this->_fields['select'][1]->render()
            .$this->_fields['button']->render()
        .'</div>';
    }
}